"""
Case Type   : CBM
Case Name   : 资源池化场景下，无任何业务压力, switchover -n 指定节点主备切换
Create At   : 2024-04-07
Owner       : songshixuan
Description :
    1、查看集群状态 cm_ctl query -Cidvp
    2、备1节点执行switchover切换为主节点 cm_ctl switchover -n 2 -D xxx/dn1
    3、再次查看切换后集群状态 cm_ctl query -Cidvp
    4、恢复集群原始状态 cm_ctl switchover -a
Expect      :
    1、集群状态正常，数据库节点状态正常
    2、switchover执行成功
    3、集群状态正常,备1节点切换为主节点，原主节点降为备节点
    4、恢复集群原始状态成功,集群状态正常
History     :
    1、2025-04-07 创建
"""
import os
import unittest
import time

from testcase.utils.ComThread import ComThread
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro
from testcase.utils.Common import Common
from yat.test import Node

Primary_SH = CommonSH('PrimaryDbUser')
Primary_ROOTSH= CommonSH('PrimaryRoot')

@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.s1_comsh = CommonSH('Standby1DbUser')
        self.param = 'enable_cbm_tracking'
        self.com = Common()
        self.tpcc_user = Node('PrimaryRoot')

    def test_shared_storage(self):
        text = '-----step1:查看集群状态; expect:集群状态正常-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)

        text = '-----step2:gs_guc set方式开启cbm 参数;  expect:修改成功-----'
        self.log.info(text)

        value = Primary_SH.execute_gsguc('set',
                                          self.constant.GSGUC_SUCCESS_MSG,
                                          f"{self.param} = on")
        self.log.info(value)
        self.assertTrue(value, '修改失败')


        text = '-----step3:开启tpcc expect:运行成功，此处不做检查-----'
        self.log.info(text)
        start_tpcc = ComThread(self.com.start_tpcc,
                                 args=(self.tpcc_user,'/data/benchmarksql-5.0/run',
                                       './runBenchmark.sh props_write.pg'))
        start_tpcc.setDaemon(True)
        start_tpcc.start()
        status = Primary_ROOTSH.exec_cmd_under_root("ps -ux | grep './runBenchmark.sh' | grep -v -- '--color'")
        self.log.info(status)
        self.assertIn('props_write.pg', status, '执行失败' + text)
        time.sleep(10)

        text = '-----step4:备1节点执行switchover切换为主节点; expect:执行成功-----'
        self.log.info(text)
        switch = self.s1_comsh.exec_cm_ctl(mode='switchover',
                                           param='-n 2',
                                           path=f'-D {macro.DB_INSTANCE_PATH}')
        self.log.info(switch)
        self.assertIn(self.constant.cm_switchover_success_msg, switch,
                      '执行失败' + text)

        text = '-----step5:再次查看切换后集群状态 expect:集群状态正常,备1切换为主节点，原主节点降为备节点-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)
        res1 = status.splitlines()[-1].split('|')[1].split('6002')[-1]
        res2 = status.splitlines()[-1].split('|')[0].split('6001')[-1]
        self.log.info(res1)
        self.log.info(res2)
        self.assertIn('S Primary Normal', res1, '执行失败' + text)
        self.assertIn('P Standby Normal', res2, '执行失败' + text)

    def tearDown(self):
        text = '-----step6:恢复环境为初始集群状态; expect:成功-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        status = Primary_SH.check_cluster_core_status(status, wait_times=600)
        self.log.info(status)
        status = Primary_SH.exec_cm_ctl(mode='stop', param='')
        self.log.info(status)
        self.assertIn('successfully.', status, '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='start', param='')
        self.log.info(status)
        self.assertIn('successfully.', status, '执行失败' + text)

        recovery = Primary_SH.exec_cm_ctl(mode='switchover', param='-a')
        self.log.info(recovery)
        self.assertIn(self.constant.cm_switchover_success_msg, recovery,
                      '执行失败' + text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)

        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)
        self.assertIn('P Primary Normal',
                      status.splitlines()[-1].split('|')[0].split('6001')[-1],
                      '执行失败' + text)
        self.assertIn('S Standby Normal',
                      status.splitlines()[-1].split('|')[1].split('6002')[-1],
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')